
#include "server_object_create.h"
#include "valuerange.hpp"
#include <ltobjectcreate.h>
#include "Pageinfogetter.hpp"

void bind_object_object_info_from_page(const std::string& page_name,
                              const std::string& objname,
                              ObjectCreateStruct* pstruct) {
    using namespace Utility ;

    LTVector vPos, vStartPos;
    LTRotation rStartRot;  
    pstruct->Clear();
    pstruct->m_ObjectType = OT_MODEL;
    pstruct->m_Flags = FLAG_VISIBLE |FLAG_FULLPOSITIONRES|FLAG_FORCECLIENTUPDATE ;
    std::string file_name =
        page_info_getter::Instance().get<std::string>(page_name,
                                                      objname,
                                                      "obj_file") ;
  
    strcpy(pstruct->m_Filenames[0], file_name.c_str()) ;
    int use_shadow = page_info_getter::Instance().get(page_name,
                                                      objname,
                                                      "use_shadow",
                                                      0) ;
    if(use_shadow)
        pstruct->m_Flags |= FLAG_SHADOW | FLAG_CASTSHADOWS;
    
	
    for(int i = 0 ; i < 32 ; i++) {
        std::string skinid = "skin" +
            boost::lexical_cast<std::string>(i) ;
        std::string skin_name =
            page_info_getter::Instance().get<std::string>(page_name,
                                                          objname,
                                                          skinid,
                                                          std::string()) ;
        if(!skin_name.empty()) {
            strcpy(pstruct->m_SkinNames[i], skin_name.c_str()) ;
        }
    }
  
    for(int i = 0 ; i < 8 ; i++) {
        std::string rsid = "rs" + boost::lexical_cast<std::string>(i) ;
        std::string rs_name =
            page_info_getter::Instance().get<std::string>(page_name,
                                                          objname,
                                                          rsid) ;
        if(!rs_name.empty()) {
            strcpy(pstruct->m_RenderStyleNames[i], rs_name.c_str()) ;
        }
    }
	

    normal_number_range scale = page_info_getter::Instance().get(
        page_name,
        objname,
        "scale",
        normal_number_range(1.0, 1.0, 1.0)) ;
    pstruct->m_Scale = LTVector(scale[0], scale[1], scale[2]) ;
}

void remove_object(HOBJECT obj) {
    if(obj)
        g_pLTServer->RemoveObject(obj) ;
}

BaseClass* create_object(const char* class_name, ObjectCreateStruct* info) {
    HCLASS hClass;

	hClass = g_pLTServer->GetClass(class_name);

	if (!hClass) {
		return NULL;
	}

	BaseClass* obj = static_cast<BaseClass*>(g_pLTServer->CreateObject(hClass, info)) ;
  return obj ;
	// Store our client
}
